Pass-by-Value
Pass-by-Value in Java (Why Java Is NOT Pass-by-Reference)β
Why This File Existsβ
βJava is pass-by-referenceβ is one of the most common incorrect statements about Java.
This file explains:
- How Java passes arguments to methods
- Why primitives and objects appear to behave differently
- The exact rule Java follows
Understanding this removes confusion in:
- method calls
- object modification
- debugging unexpected behavior
The Golden Rule (Memorize This)β
Java is ALWAYS pass-by-value
No exceptions.
The confusion comes from what value is passed.
What Pass-by-Value Meansβ
Pass-by-value means:
- A copy of the value is passed to the method
- The original variable is not directly accessible
The key question is:
What is the value?
Case 1: Passing Primitive Typesβ
void update(int x) {
x = 20;
}
int a = 10;
update(a);
System.out.println(a);
Output:
10
Explanation:
- Value
10is copied intox - Changing
xdoes not affecta
Simple and intuitive.
Case 2: Passing Object Referencesβ
void updateName(Person p) {
p.name = "John";
}
Person p1 = new Person();
p1.name = "Alex";
updateName(p1);
System.out.println(p1.name);
Output:
John
This creates confusion.
Why Objects Appear Pass-by-Referenceβ
What actually happens:
- The reference value is copied
- Both variables point to the same object
So:
- Object state can be modified
- Reference itself cannot be changed
Proof: Reference Reassignment Failsβ
void reassign(Person p) {
p = new Person();
p.name = "Bob";
}
Person p1 = new Person();
p1.name = "Alex";
reassign(p1);
System.out.println(p1.name);
Output:
Alex
Explanation:
- Reference copy is reassigned
- Original reference remains unchanged
Visual Summaryβ
Primitive:
a βββΊ 10 (copied)
Object:
p1 βββΊ [Person]
p βββΊ [Person] (same object, copied reference)
Why Java Chose Pass-by-Valueβ
- Simpler language model
- Avoids unpredictable side effects
- More secure and consistent behavior
Pass-by-reference would make Java error-prone.
Common Misconceptionsβ
β Java passes objects by reference
β Java passes object references by value
β Objects behave like references
β Only references are copied
Why This Mattersβ
Understanding this helps you:
- predict method behavior
- avoid unintended mutations
- write safer APIs
- answer interview questions correctly
Common Mistakesβ
- Claiming Java is pass-by-reference
- Expecting reassignment to affect caller
- Not understanding reference copying
Best Practicesβ
- Avoid mutating objects unnecessarily
- Be explicit about method side effects
- Prefer immutability where possible
Interview Notesβ
- Is Java pass-by-value or pass-by-reference?
- Why do objects behave differently than primitives?
- Can a method change the callerβs object reference?
- Explain with an example
Summaryβ
Java passes:
- primitive values β by value
- object references β by value
Java is always pass-by-value.